package com.zyk.scaffold.core.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.zyk.scaffold.core.component.CountRedisLimitComponent;
import com.zyk.scaffold.core.component.RateRedisLimitComponent;
import com.zyk.scaffold.core.component.RedisSlideLimitComponent;
import com.zyk.scaffold.core.component.SimpleCountRedisLimitComponent;
import com.zyk.scaffold.core.lock.LockComponent;
import com.zyk.scaffold.core.lock.ReentrantRedisLock;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.client.loadbalancer.AsyncLoadBalancerAutoConfiguration;
import org.springframework.cloud.client.loadbalancer.AsyncRestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.client.AsyncRestTemplate;

import java.util.List;

@Configuration
@ConditionalOnClass(value = {StringRedisTemplate.class, RedisTemplate.class})
public class RedisComponentConfiguration {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        // JSON序列化配置 不需要被,不需要知道具体参数含义 大概知道是做什么的就可以 这个就是采用JSON序列化对象
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer  = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // String的序列化配置
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // 所有的Key通过String序列化
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        // 所有的value通过JSON序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        // 调用刚才看的序列化源码中默认的方法,重新设置序列化
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template=new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    public RedisSlideLimitComponent redisSlideLimitComponent(StringRedisTemplate stringRedisTemplate){
        return new RedisSlideLimitComponent(stringRedisTemplate);
    }

    @Bean
    public CountRedisLimitComponent countRedisLimitComponent(StringRedisTemplate stringRedisTemplate){
        return new CountRedisLimitComponent(stringRedisTemplate);
    }

    @Bean
    public SimpleCountRedisLimitComponent simpleCountRedisLimitComponent(StringRedisTemplate stringRedisTemplate){
        return new SimpleCountRedisLimitComponent(stringRedisTemplate);
    }

    @Bean
    public RateRedisLimitComponent rateRedisLimitComponent(StringRedisTemplate stringRedisTemplate){
        return new RateRedisLimitComponent(stringRedisTemplate);
    }

    @Bean
    public LockComponent lockComponent(StringRedisTemplate stringRedisTemplate){
        return new LockComponent(stringRedisTemplate);
    }

    @Bean
    public ReentrantRedisLock reentrantRedisLock(RedisTemplate redisTemplate, ConfigurableEnvironment environment){
        String ipAddress = environment.getProperty("spring.cloud.client.ip-address", "127.0.0.1");
        Integer port = environment.getProperty("server.port", Integer.class, RandomUtils.nextInt(1000, 20000));
        return new ReentrantRedisLock(redisTemplate, ipAddress + "_" + port);
    }
}
